package spring.superrun.record.mvc.entity.response;

import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;

import java.util.Date;

/**
 * 通用相应模板
 * hby
 */

public class ServiceResult {
    /// <summary>
    /// 响应码
    /// </summary>
    private ServiceResultCode Code ;
    /// <summary>
    /// 响应信息
    /// </summary>
    private String Message ;



    /// <summary>
    /// 通用字符数据
    /// </summary>
    private String temp ;


    public boolean isSuccess() {
        return Success;
    }

    public void setSuccess() {
      Success=  getCode() == ServiceResultCode.Succeed;
    }

    /// <summary>
    /// 成功
    /// </summary>
    public boolean Success;

    public long getTimestamp() {
        return Timestamp;
    }

    public void setTimestamp() {
        Timestamp = System.currentTimeMillis(); // 13位数的时间戳
    }

    /// <summary>
    /// 时间戳(毫秒)
    /// </summary>
    private long Timestamp  ;
    /// <summary>
    /// 响应成功
    /// </summary>
    /// <param name="message"></param>
    /// <returns></returns>
    public void IsSuccess(String message)
    {
        Message = message;
        Code = ServiceResultCode.Succeed;
        setSuccess();
    }

    /// <summary>
    /// 响应失败
    /// </summary>
    /// <param name="message"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public void IsFailed(String message)
    {
        Message = message;
        Code = ServiceResultCode.Failed;
        setSuccess();
    }
    /// <summary>
    /// 响应失败
    /// </summary>
    /// <param name="code"></param>
    /// <param name="message"></param>
    public void IsFailed(ServiceResultCode code, String message)
    {
        Code = code;
        Message = message;
        setSuccess();
    }
    /// <summary>
    /// 可返回本体的方法
    /// </summary>
    /// <param name="isReturn"></param>
    /// <param name="code"></param>
    /// <param name="message"></param>
    public ServiceResult IsFailed(Boolean isReturn, ServiceResultCode code, String message)
    {
        Code = code;
        Message = message;
        setSuccess();
        return this;
    }
    /// <summary>
    /// 响应失败
    /// </summary>
    /// <param name="exception"></param>
    /// <returns></returns>
    public void IsFailed(Exception exception)
    {
        Message = exception.getMessage();//InnerException?.StackTrace
        //添加日志
        Code = ServiceResultCode.Failed;
        setSuccess();
    }
    public ServiceResultCode getCode() {
        return Code;
    }

    public void setCode(ServiceResultCode code) {
        Code = code;
    }
    public String getMessage() {
        return Message;
    }

    public void setMessage(String message) {
        Message = message;
    }
    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }
}
